/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleLearningMap.controller("LearningMapRouteDetail", function (LanguageService, $$CambioConfig, UploadFileService, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService, LearningMapRouteResource, LearningMapCourseRouteResource) {
    $scope.$on("$viewContentLoaded", function () {
        //$scope.reset();
        pageOnLoad();
        ConstantService.addState($state.current, true, LanguageService.language('学习地图模版详细'));
        //$scope.learningMapCourseId = $stateParams.learningMapCourseId;
        $scope.learningMapRouteId = $stateParams.learningMapRouteId;
        $scope.selectTab('tabInfo');

        $scope.currentCourseRoute = null;
    });

    $scope.currentTabId = 0;
    $scope.selectTab = function (tabId) {
        if (tabId === 'tabInfo') {
            $scope.loadDetail();
            $scope.currentTabId = tabId;
        }
        if (tabId === 'tabCourseRoute' && $scope.learningMapRouteId) {
            $scope.loadCourseRoute();
            $scope.currentTabId = tabId;
        }
    };

    // ------------------------------------------------------------------------------------------------------

    $scope.queryPara = {
        className: Models.LearningMapRoute.className
    };

    $scope.loadDetail = function () {
        if ($scope.learningMapRouteId) {
            //LoadingService.show();
            GeneralResource.findById({id: $scope.learningMapRouteId}, $scope.queryPara, function (queryResult) {
                $log.debug("查询结果", queryResult.data);
                $scope.learningMapRouteDetail = queryResult.data;
                //LoadingService.hide();
            });
        } else {
            $scope.learningMapRouteDetail = {};
        }
    };

    $scope.saveRoute = function () {

        Alert.confirm(function () {
            if ($scope.learningMapRouteId) {
                LearningMapRouteResource.update({}, $scope.learningMapRouteDetail, function (queryResult) {
                    $log.debug("查询结果", queryResult.data);
                    //$scope.learningMapCourseDetail = queryResult.data;
                    Alert.success();
                    $scope.loadDetail();
                    //LoadingService.hide();
                });
            } else {
                LearningMapRouteResource.save({}, $scope.learningMapRouteDetail, function (queryResult) {
                    $log.debug("查询结果", queryResult.data);
                    $scope.learningMapRouteId = queryResult.data.value;
                    Alert.success();
                    $state.go("sz.learningMapRouteDetail", {
                        learningMapRouteId: $scope.learningMapRouteId
                    });
                    //LoadingService.hide();
                });
            }
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.loadCourseRoute = function () {
        $scope.queryCourseRoutePara = {
            className: Models.LearningMapCourseRoute.className,
            query: {
                p1: {
                    field: Models.LearningMapCourseRoute.query.learningMapRouteId,
                    value: $scope.learningMapRouteId
                }
            }
        };

        //LoadingService.show();
        GeneralResource.queryPage({pageNo: 0, pageSize: 50}, $scope.queryCourseRoutePara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.courseRoutes = queryResult.data.datas;
            UUID.format($scope.courseRoutes);
            $scope.initImg();
            //LoadingService.hide();
        });
    }

    $scope.selectCourseRoute = function (courseRoute) {
        for (var i = 0; i < $scope.courseRoutes.length; i++) {
            $scope.courseRoutes[i].tr = {};
            $scope.courseRoutes[i].courseRouteImg.img = 'skin/default/img/courseRoute.png';
            //document.write($scope.courseRoutes[i] + "<br>");
        }
        courseRoute.tr = {
            'background-color': 'wheat',
            'background': 'wheat'
        }
        courseRoute.courseRouteImg.img = 'skin/default/img/currentCourseRoute.png';
        $scope.currentCourseRoute = courseRoute;
    }

    $scope.changeCourseRoute = function (courseRoute, $event) {
        var result = mapOnClick();
        $scope.currentCourseRoute.x = toFull(result.x);
        $scope.currentCourseRoute.y = toFull(result.y);
        $scope.currentCourseRoute.courseRouteImg.top = result.y - 30 + "px";
        $scope.currentCourseRoute.courseRouteImg.left = result.x - 30 + "px";
    }

    $scope.changeCourseRouteImg = function ($event) {
        var result = mapOnClick();
        result.y = toZip($scope.currentCourseRoute.y) + result.y - 30;
        result.x = toZip($scope.currentCourseRoute.x) + result.x - 30;
        $scope.currentCourseRoute.x = toFull(result.x);
        $scope.currentCourseRoute.y = toFull(result.y);
        $scope.currentCourseRoute.courseRouteImg.top = result.y - 30 + "px";
        $scope.currentCourseRoute.courseRouteImg.left = result.x - 30 + "px";
    }

    $scope.initImg = function () {
        for (var i = 0; i < $scope.courseRoutes.length; i++) {
            var courseRouteImg = {
                top: toZip($scope.courseRoutes[i].y) - 30 + "px",
                left: toZip($scope.courseRoutes[i].x) - 30 + "px",
                img: 'skin/default/img/courseRoute.png'
            };
            $scope.courseRoutes[i].courseRouteImg = courseRouteImg;
            //document.write($scope.courseRoutes[i] + "<br>");
        }
    }

    /**
     * 转换成压缩后的坐标
     */
    function toZip(value) {
        var score = 0;
        score = 400 / $scope.learningMapRouteDetail.width;
        return parseInt(value * score);
    }

    /**
     * 转换成原始坐标
     */
    function toFull(value) {
        var score = 0;
        score = 400 / $scope.learningMapRouteDetail.width;
        return parseInt(value / score);
    }


    $scope.addCourseRoute = function () {
        var sort = 1;
        if ($scope.courseRoutes) {
            sort = $scope.courseRoutes.length + 1
        }

        $scope.currentCourseRoute = {
            sort: sort,
            x: 0,
            y: 0
        }
        if (!$scope.courseRoutes) {
            $scope.courseRoutes = new Array();
        }
        $scope.courseRoutes.push($scope.currentCourseRoute);
        $scope.initImg();
        UUID.format($scope.courseRoutes);
        $scope.selectCourseRoute($scope.currentCourseRoute);
    }

    $scope.editCourseRoute = function (courseRoute) {
        $scope.currentCourseRoute = courseRoute;
        DialogService.open("learningMapCourseRouteMgt", $scope);
    }

    $scope.removeCourseRoute = function (courseRoute, index) {
        $scope.courseRoutes.removeForUid(courseRoute);
        //for (var i = 0; i < $scope.courseRoutes.length; i++) {
        //    if ($scope.courseRoutes[i].uid == courseRoute.uid) {
        //        $scope.courseRoutes.remove(i);
        //        return;
        //    }
        //}
    }

    function mapOnClick(e) {
        e = e || window.event;
        var target = $(e.target);
        var offsetX = e.pageX - target.offset().left;
        var offsetY = e.pageY - target.offset().top;
        var x = parseInt(offsetX);
        var y = parseInt(offsetY);
        return {
            x: x,
            y: y
        }
    }

    $scope.saveCourseRoute = function () {
        //LearningMapCourseRouteResource
        Alert.confirm(function () {
            LearningMapCourseRouteResource.save({learningMapRouteId: $scope.learningMapRouteId}, $scope.courseRoutes, function (queryResult) {
                $log.debug("查询结果", queryResult.data);
                //$scope.learningMapCourseDetail = queryResult.data;
                Alert.success();
                $scope.selectTab('tabInfo');
                //LoadingService.hide();
            });
        })
    }

    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------

    $scope.uploadImage = function ($files) {
        if ($files && $files[0]) {
            UploadFileService.uploadFile($files[0], $$CambioConfig.UPLOAD_URL, 'IMG', $scope).then(function (o) {
                $scope.learningMapRouteDetail.picture = o.id;
            });
        }
    };

});